package cn.edu.neosoft.sunbo509.finefood.Fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.edu.neosoft.sunbo509.finefood.Activity.MycommentActivity;
import cn.edu.neosoft.sunbo509.finefood.Activity.OrderActivity;
import cn.edu.neosoft.sunbo509.finefood.Activity.SettingActivity;
import cn.edu.neosoft.sunbo509.finefood.Activity.UserActivity;
import cn.edu.neosoft.sunbo509.finefood.BaseUrl;
import cn.edu.neosoft.sunbo509.finefood.Bean.UserBean;
import cn.edu.neosoft.sunbo509.finefood.R;
import cn.edu.neosoft.sunbo509.finefood.Service.Service;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link UserFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link UserFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class UserFragment extends Fragment implements View.OnClickListener {
    private View view;
    private LinearLayout ll_myorder,ll_mycomment;
    private RelativeLayout rl_msg;
    private TextView tv_username,tv_userphone;
    private Retrofit retrofit;
    private Service service;
    private String user_id;
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public UserFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment UserFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static UserFragment newInstance(String param1, String param2) {
        UserFragment fragment = new UserFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view=inflater.inflate(R.layout.fragment_user, container, false);
        setHasOptionsMenu(true);
        Bundle bundle=getArguments();
        user_id=bundle.getString("userid");
        InitView();
        getDate();
        return  view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.userset, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    private void InitView() {
        ll_myorder=(LinearLayout)view.findViewById(R.id.ll_myorder);
        ll_mycomment=(LinearLayout)view.findViewById(R.id.ll_mycomment);
        rl_msg=(RelativeLayout)view.findViewById(R.id.rl_msg);
        tv_username=(TextView)view.findViewById(R.id.username);
        tv_userphone=(TextView)view.findViewById(R.id.userphone);
        ll_mycomment.setOnClickListener(this);
        ll_myorder.setOnClickListener(this);
        rl_msg.setOnClickListener(this);
    }


    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_msg:
                Intent intent=new Intent(getActivity(),UserActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_myorder:
                Intent intent1=new Intent(getActivity(), OrderActivity.class);
                intent1.putExtra("userid",user_id);
                startActivity(intent1);
                break;
            case R.id.ll_mycomment:
                Intent intent2=new Intent(getActivity(), MycommentActivity.class);
                intent2.putExtra("userid",user_id);
                startActivity(intent2);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.userset:
                Intent intent=new Intent(getActivity(), SettingActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */

    //获取用户信息
        public void getDate(){
            retrofit=new Retrofit.Builder()
                    .baseUrl(BaseUrl.BASEURL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            service=retrofit.create(Service.class);
            Call<UserBean>call=service.getUser(user_id);
            call.enqueue(new Callback<UserBean>() {
                @Override
                public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                    tv_userphone.setText(response.body().getMobilenum());
                    tv_username.setText(response.body().getUsername());
                }
                @Override
                public void onFailure(Call<UserBean> call, Throwable t) {
                    Log.e("error",t.getMessage());
                }
            });

        }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
